﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;


namespace RemoteDesktops
{
    class RemoteDesktopConnection
    {



        public string location = "", description = "", domain = "", username = "", computer = "";
        public int width = 1024, height = 768;
        public bool admin = false, span = false;

        public RemoteDesktopConnection()
        {

        }

        public void run()
        {
            string filename;
            if (username.Length > 5)
                filename = username.Substring(0,5) + ".rdp";
            else
                filename = username + ".rdp";
           // if (filename.Contains("\\")) 
           //     filename = filename.Split('\\').Last();

            FileInfo f = new FileInfo(filename);
            StreamWriter sw = f.CreateText();
            sw.WriteLine("screen mode id:i:1");
            sw.WriteLine("desktopwidth:i:" + width);
            sw.WriteLine("desktopheight:i:" + height);
            sw.WriteLine("session bpp:i:32");
            sw.WriteLine("winposstr:s:0,1,0,0," + (width+20) + "," + (height+50) + "");
            sw.WriteLine("compression:i:1");
            sw.WriteLine("keyboardhook:i:2");
            sw.WriteLine("displayconnectionbar:i:1");
            sw.WriteLine("disable wallpaper:i:1");
            sw.WriteLine("disable full window drag:i:1");
            sw.WriteLine("allow desktop composition:i:0");
            sw.WriteLine("allow font smoothing:i:0");
            sw.WriteLine("disable menu anims:i:1");
            sw.WriteLine("disable themes:i:0");
            sw.WriteLine("disable cursor setting:i:0");
            sw.WriteLine("bitmapcachepersistenable:i:1");
            sw.WriteLine("full address:s:" + computer);
            sw.WriteLine("audiomode:i:0");
            sw.WriteLine("redirectprinters:i:1");
            sw.WriteLine("redirectcomports:i:0");
            sw.WriteLine("redirectsmartcards:i:1");
            sw.WriteLine("redirectclipboard:i:1");
            sw.WriteLine("redirectposdevices:i:0");
            sw.WriteLine("autoreconnection enabled:i:1");
            sw.WriteLine("authentication level:i:0");
            sw.WriteLine("prompt for credentials:i:1");
            sw.WriteLine("negotiate security layer:i:1");
            sw.WriteLine("remoteapplicationmode:i:0");
            sw.WriteLine("alternate shell:s:");
            sw.WriteLine("shell working directory:s:");
            sw.WriteLine("gatewayhostname:s:");
            sw.WriteLine("gatewayusagemethod:i:4");
            sw.WriteLine("gatewaycredentialssource:i:4");
            sw.WriteLine("gatewayprofileusagemethod:i:0");
            sw.WriteLine("promptcredentialonce:i:1");
            sw.WriteLine("redirectdrives:i:0");
            sw.WriteLine("drivestoredirect:s:");
            if (domain.Length < 1)
                sw.WriteLine("username:s:" + username);
            else
                sw.WriteLine("username:s:" + domain + "\\" + username);
            sw.Close();


            System.Diagnostics.ProcessStartInfo psi =
                new System.Diagnostics.ProcessStartInfo(@"mstsc.exe");
            psi.Arguments = filename;
            if (admin) psi.Arguments += " /admin";
            if (span) psi.Arguments += " /span";
            //psi.RedirectStandardOutput = true;
            //psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            //psi.UseShellExecute = false;
            System.Diagnostics.Process process;
            process = System.Diagnostics.Process.Start(psi);
        }
    }
}




/*
A new policy setting is available in Windows XP and in Windows 2000 that you can use to control whether users can save passwords in a Remote Desktop Connection session. After you apply the hotfix that is described in this article, you can configure the "Do not allow passwords to be saved" policy setting to either allow or not allow users to save passwords in a Remote Desktop Connection session. You can use Group Policy to apply the policy setting to Windows XP and Windows 2000-based computers in your domain environment. Or, you can apply the policy setting locally.

To configure this policy setting, follow these steps:

   1. Open the appropriate policy object, and then use one of the following methods:
          * To apply the policy to the computer, expand Computer Configuration, expand Administrative Templates, expand Windows Components, and then expand Terminal Services.
          * To apply the policy to users, expand User Configuration, expand Administrative Templates, expand Windows Components, and then expand Terminal Services.
   2. Click Client.
   3. In the right pane, double-click Do not allow passwords to be saved, and then use one of the following methods:
          * To enable the policy setting so that users cannot save passwords, click Enabled.

            Note After you enable the "Do not allow passwords to be saved" policy setting, the Save my password check box on the General tab in the Remote Desktop Connection dialog box is not available (appears dimmed).
          * To disable the policy setting so that users can save passwords, click Disabled.
   4. Click OK.

The policy setting is stored in the DisablePasswordSaving registry entry in the following registry subkey:
HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows NT\Terminal Services
When you enable the "Do not allow passwords to be saved" setting so that users cannot save passwords, the DisablePasswordSaving registry entry is set to 1. When you disable this setting so that users can save passwords, the DisablePasswordSaving registry entry is set to 0 (zero).

Note

    * Windows 2000 with Service Pack 4 (SP4) cannot save passwords in a Remote Desktop Connection session to a terminal server. Apply this hotfix or apply Update rollup 1 for Windows 2000 SP4 to enable the feature.
    * Remote Desktop Connection 5.1 for Windows XP with Service Pack 1 (SP1) cannot save passwords. Apply this hotfix to enable the feature.
    * Remote Desktop Connection 5.2 for Windows XP with Service Pack 2 (SP2) can save passwords.
    * Remote Desktop Connection 5.2 for Microsoft Windows Server 2003 and for Windows Server 2003 SP1 can save passwords.
    * Only Windows Server 2003 with SP1 supports authentication in this scenario and has a Security tab where you can set options. 
*/